package com.yxy.springcloud.framework.unionauth.controller;

import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.Pagingable;
import com.yxy.springcloud.framework.common.base.RestResponse;
import com.yxy.springcloud.framework.unionauth.DTO.*;
import com.yxy.springcloud.framework.unionauth.model.Tenant;
import com.yxy.springcloud.framework.unionauth.service.RoleService;
import com.yxy.springcloud.framework.unionauth.service.TenantService;
import com.yxy.springcloud.framework.unionauth.utils.CheckAppPermission;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yuxiaoyang
 * 租户相关接口
 */
@RestController
@RequestMapping("/tenant")
public class TenantController {
  @Resource
  private TenantService functionService;

  // 该list接口 供具体的app调用,用于获取该app下所有的租户
  @PostMapping("/list")
  public RestResponse getList(@RequestBody TenantListDto query) {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getList(query));
    return restResponse;
  }

  // 该list2接口 供vue-union-auth前端调用,用于获取所有的租户列表
  @PostMapping("/list2")
  public RestResponse getList2(@RequestBody TenantListDto query) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getList2(query));
    return restResponse;
  }

  //根据主键获取单条记录详情
  @GetMapping("/getTenantById")
  public RestResponse<Tenant> getTenantById(@RequestParam("id") long id) {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getDetailById(id));
    return restResponse;
  }

  @PostMapping("/createOrUpdate")
  public RestResponse createOrUpdate(@RequestBody TenantCreateDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.createOrUpdate(inputDto));
    return restResponse;
  }

  /**
   * 给app后台调用,在app端创建or修改租户
   * @param inputDto
   * @return
   * @throws BusinessException
   */
  @CheckAppPermission
  @PostMapping("/createOrUpdate2")
  public RestResponse createOrUpdate2(@RequestBody TenantCreateDto2 inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.createOrUpdate2(inputDto));
    return restResponse;
  }

  @DeleteMapping("/{id}")
  public RestResponse delete(@PathVariable("id") long id) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    functionService.delete(id);
    return restResponse;
  }

  // 禁用or 启用某个tenant下所有user的登录功能
  @GetMapping("/forbidAllUserLogin")
  public RestResponse forbidAllUserLogin(@RequestParam("tenantId") Long tenantId, @RequestParam("flag") String flag) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    functionService.forbidAllUserLogin(tenantId, flag);
    return restResponse;
  }

}
